package com.mfp.java.testYou.ui.mvc;

import java.util.LinkedList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mfp.java.testYou.core.AppContext;
import com.mfp.java.testYou.core.servlets.TakeTestServlet;
import com.mfp.java.testYou.core.utils.UrlUtils;
import com.mfp.java.testYou.db.model.Test;
import com.mfp.java.testYou.model.UiSelectPair;

public class UserMenuPage extends AbstractMVC {

	public static final String URL = "/testYou/pages/userMenu.jsp";

	private static final Integer LIST_SIZE = 5;

	public UserMenuPage(HttpServletRequest request, HttpServletResponse response) {
		super(request, response);
		init();
		initList();
	}

	private void init() {

	}

	private void initList() {
		List<Test> list = AppContext.getDAO().getFinalTests();
		List<UiSelectPair> uiList = new LinkedList<UiSelectPair>();
		for (Test t : list) {
			uiList.add(new UiSelectPair(t.getTitle(), t.getId()));
		}

		getRequest().setAttribute(SelectListComponent.ATTR_LIST_DATA, uiList);
		getRequest().setAttribute(SelectListComponent.ATTR_LIST_NAME, TakeTestServlet.INPUT_TESTID);
		getRequest().setAttribute(SelectListComponent.ATTR_LIST_SIZE, LIST_SIZE);
	}

	public boolean hasCurrentTest() {
		if (AppContext.getUserSession(super.getRequest()).getTakenTest() != null) {
			return true;
		}
		return false;
	}

	public String getContTestUrl() {
		String params = UrlUtils.makeParamsString(TakeTestServlet.OPERATION, TakeTestServlet.OPERATION_ANSWER);
		String url = TakeTestServlet.URL;
		return url + params;
	}

}
